package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

/**
 *  hf365NewsAnalyse  合肥在线新闻解析类
 *  xiaonie 2012-11-22
 */
public class hf365NewsAnalyse implements AnalyseNews{
	private final String[] regexs={ "http://[a-z]*[.]hf365.com/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]+.shtml" };

	
	public boolean isDetailPage(String url) {
		for (String regex : regexs) {
			if (url.matches(regex)) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String str_encode=urlMeta.getEncoding();
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		Long date=null;
		String dateStr=null;

		if (htmltxt != null && htmltxt != "") {
			news.setUrl(url);
			if(url.startsWith("http://ent.hf365.com/")){
				try {
					title=doc.select("span.biaoti").text();
					content=HtmlCleaner.getContentHtml(url,doc.select("span.zhengwen"));
					Elements select=doc.select("table[width=580]");
					Element element=select.get(0);
					dateStr=element.select("td[align=right]").text();
				} catch (Exception e) {
					try {
						title=doc.select("h1").text();
						content=HtmlCleaner.getContentHtml(url,doc.select("div.main_con"));
						dateStr=doc.select("div.time").text();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}else{
				title=doc.select("h1").text();
				if(StringUtils.isBlank(title)){
					title=doc.select("span.biaoti").text();
				}
				content=HtmlCleaner.getContentHtml(url,doc.select("div.main_con"));
				
				dateStr=doc.select("div.time").text();
				if(StringUtils.isBlank(dateStr)){
					dateStr=url;
				}
			}
			if(title == null || title.trim().length() == 0){
			}
			date=DateUtils.matchDate(dateStr);
			news.setContent(content);
			news.setTitle(title);
			news.setClickNum(null);
			news.setCommentNum(null);
			news.setDate(date);
			news.setType(1);
			
			String author=doc.select("div#con div.main div.time a").text();
			if(StringUtils.isBlank(author)){
				author=doc.select("div.time").text();
			}
			author=StringUtils.substringAfter(author, "稿源：");
			if(StringUtils.isNotBlank(author)){
				author=author.trim();
			}
			if("合肥在线".equals(author)){
				author=null;
			}
			
			news.setAuthor(author);
		}
		return news;
	}

	public static void main(String[] args) {
		hf365NewsAnalyse a=new hf365NewsAnalyse();
		String url="http://news.hf365.com/system/2014/03/28/013866241.shtml";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(a.isDetailPage(url));
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
